termDocMatrix <- m2;
#inspect part of the matrix
termDocMatrix[5:10, 1:20];
#change it to a Boolean matrix
termDocMatrix[termDocMatrix>=1] <- 1;
#transform into a term-term adjacency matrix
termMatrix <- termDocMatrix %*% t(termDocMatrix);
#inspect terms numbered 5 to 10
termMatrix[5:10, 5:10];

#*******draw graph *************
library(igraph);
#build a graph from the above matrix
g <- graph.adjacency(termMatrix, weighted=T, mode="undirected");
#remove loops
g <- simplify(g);
#set labels and degrees of vertices
V(g)$label <- V(g)$name;
V(g)$degree <- degree(g);
#set seed to make the layout reproducible
set.seed(3952);
layout1 <- layout.fruchterman.reingold(g);
plot(g, layout=layout1);

#** save as pdf **
#png("term-network.png");
plot(g, layout=layout.fruchterman.reingold);
dev.off()


#*********************draw the terms network ***********************
V(g)$label.cex <- 2.2 * V(g)$degree / max(V(g)$degree) + .2;
V(g)$label.color <- rgb(0, 0, .2, .8);
V(g)$frame.color <- NA;
egam <- (log(E(g)$weight) + .4) / max(log(E(g)$weight) + .4);
E(g)$color <- rgb(.5, .5, 0, egam);
E(g)$width <- egam;
#plot the graph in layout1
plot(g, layout=layout1);

#*********************draw the document network *********************
M <- termDocMatrix;
#build a doc-doc adjacency matrix
tweetMatrix <- t(M) %*% M;
g2 <- graph.adjacency(tweetMatrix, weighted=T, mode="undirected");
V(g2)$degree <- degree(g2);
g2 <- simplify(g2);
#set labels of vertices to tweet IDs
V(g2)$label <- V(g2)$name;
V(g2)$label.cex <- 1;
V(g2)$label.color <- rgb(.4, 0, 0, .7);
V(g2)$size <- 2;
V(g2)$frame.color <- NA;

barplot(table(V(g2)$degree))
title(xlab="Degree")#横轴名字
title(ylab="Count")#纵轴名字


idx <- V(g2)$degree == 0;
V(g2)$label.color[idx] <- rgb(0, 0, .3, .7);
#load init text :mydataframe
df <- do.call("rbind", lapply(mydataframe, as.data.frame))
V(g2)$label[idx] <- paste(V(g2)$name[idx], substr(df$text[idx], 1, 20), sep=": ");
egam <- (log(E(g2)$weight) + .2) / max(log(E(g2)$weight) + .2);
E(g2)$color <- rgb(.5, .5, 0, egam);
E(g2)$width <- egam;
set.seed(3152);
layout2 <- layout.fruchterman.reingold(g2);
plot(g2, layout=layout2);
#delete isolate vertices
g2 <- delete.vertices(g2, V(g2)[degree(g2) == 0]);
plot(g2, layout=layout.fruchterman.reingold);

#delete small weight edges
g3 <- delete.edges(g2, E(g2)[E(g2)$weight <=1]);
g3 <- delete.vertices(g3, V(g3)[degree(g3) == 0]);
plot(g3, layout=layout.fruchterman.reingold);

#*****************draw the document <-> terms network******************
#create a graph
g <- graph.incidence(termDocMatrix, mode=c("all"));
#get index for term vertices and tweet vertices
nTerms <- nrow(M);
nDocs <- ncol(M);
idx.terms <- 1:nTerms;
idx.docs <- (nTerms + 1):(nTerms + nDocs);
#set colors and sizes for vertices
V(g)$degree <- degree(g);
V(g)$color[idx.terms] <- rgb(0, 1, 0, .5);
V(g)$size[idx.terms] <- 6;
V(g)$color[idx.docs] <- rgb(1, 0, 0, .4);
V(g)$size[idx.docs] <- 4;
V(g)$frame.color <- NA;
#set vertex labels and their colors and sizes
V(g)$label <- V(g)$name;
V(g)$label.color <- rgb(0, 0, 0, 0.5);
V(g)$label.cex <- 1.4 * V(g)$degree / max(V(g)$degree) + 1;
#set edges width and color
E(g)$width <- .3;
E(g)$color <- rgb(.5, .5, 0, .3);
set.seed(958);

plot(g, layout=layout.fruchterman.reingold);

#find neignbor
rdmVertices <- V(g)[nei("续航")];
mydataframe$text[as.numeric(rdmVertices$label)]
